// For the first version we will maintain only single connection
package com.ats.impl.tws

import com.ib.client.EClientSocket
import com.ib.client.EWrapper
import com.ats.api.model.RequestHistoricalData


class TwsAdapter {

  var host: String = "localhost";
  var port: Int = 7496;
  var clientId: Int = 6;

  val ewadapterEWrapperAdapter = new EWrapperAdapter();
  val ewrapper: EWrapper = new EWrapperImpl(ewadapterEWrapperAdapter);
  val clientSocket: EClientSocket = new EClientSocket(ewrapper);

  def isConnected(): Boolean = {
    clientSocket.isConnected();
  }

  def close() {
    clientSocket.eDisconnect();
  }

  def connect() {
    if (isConnected()) return;
    clientSocket.eConnect(host, port, clientId);

  }

  def disconnect() {
    clientSocket.eDisconnect();
  }

  //public synchronized void reqHistoricalData(int tickerId, com.ib.client.Contract contract, java.lang.String endDateTime, java.lang.String durationStr, java.lang.String barSizeSetting, java.lang.String whatToShow, int useRTH, int formatDate) { /* compiled code */ }
  def reqHistoricalData(reqHData: RequestHistoricalData) {
    clientSocket.reqHistoricalData(reqHData.tickerId, reqHData.contract, reqHData.endDateTimeAsStr, reqHData.durationStr, reqHData.barSizeSetting, reqHData.whatToShow, reqHData.useRTH, reqHData.formatDate);
  }

  def requestMarketData() {

  }

}